<template>
  <div v-show="show" class="backtop" @click="backtop">
    <slot> 顶部 </slot>
  </div>
</template>

<script>
export default {
  props: {
    top: {
      type: Number,
      default: 800,
    },
  },
  data(){
      return {
          show:false
      }
  },
  methods: {
    backtop() {
      document.documentElement.scrollTop = document.body.scrollTop = 0;
    },
    scrollHandle() {
      let scrollTop =document.documentElement.scrollTop || document.body.scrollTop;
        
      if (scrollTop >= this.top) {
        this.show = true;
      } else {
        this.show = false;
      }
    },
  },
  mounted() {
    console.log("滚轮活动中");
       document.addEventListener('scroll',this.scrollHandle)
      
  },
  unmounted() {
    console.log("销毁");
    document.removeEventListener("scroll", this.scrollHandle)
  },
  activated(){
  console.log("滚轮活动中");
     document.addEventListener('scroll',this.scrollHandle)
  },
  deactivated(){
       console.log("销毁");

    document.removeEventListener("scroll", this.scrollHandle)
  }
};
</script>

<style lang="scss" scoped>
.backtop {
  position: fixed;
  right: 0.5rem;
  bottom: 7rem;
  width: 1.5rem;
  height: 1.5rem;
  font-size: 0.5rem;
  padding: 0.25rem;
  text-align: center;
  border-radius: 50%;
  background-color: rgb(255, 255, 255);
  border: 1px solid #ccc;
  box-shadow: 0 0 8px #ccc;
  color: #ccc;
  box-sizing: border-box;
}
</style>